﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.TcpUdp;
using System.Reflection;
using Framework.Kit;

namespace BLL.Dataprogram
{
    /// <summary>
    /// 报文分析
    /// </summary>
    public class DataproAnaly
    {
        /// <summary>
        /// 报文头命令
        /// </summary>
        commandType _cmdType;
        /// <summary>
        /// 报文主体密钥
        /// </summary>
        string _dataKey;
        /// <summary>
        /// 报文主体长度
        /// </summary>
        int _dataLength;
        /// <summary>
        /// 报文结束标记
        /// </summary>
        string _dataEnd;
        /// <summary>
        /// 报文主体
        /// </summary>
        string _dataBody;

        Coder coder = new Coder();


        /// <summary>
        /// 构造函数
        /// </summary>
        public DataproAnaly()
        {
            //
        }

        /// <summary>
        /// 编码报文内容
        /// </summary>
        /// <returns>编码后的报文整体</returns>
        public virtual string encodeData()
        {
            StringBuilder data = new StringBuilder();
            //报文头命令装载
            data.Append(CmdType.ToString());
            //报文密钥加载
            data.Append(DataKey.ToString());
            //报文装载主体大小
            DataLength = DataLength = DataBody.Count().ToString();

            data.Append(DataLength.ToString());
            //报文装载主体
            data.Append(DataBody);
            return data.ToString() + DataEnd;
        }

        /// <summary>
        /// 解码报文内容
        /// </summary>
        /// <param name="dp"></param>
        public virtual void uncodeData(string dp)
        {
            if (dp == null || dp == "")
            {
                return;
            }
            try
            {
                //执行包体解析
                CmdType = (commandType)Enum.Parse(typeof(commandType), dp.Substring(0, 4).ToString(), true);
                DataKey = dp.Substring(4, 8);
                DataLength = dp.Substring(12, 4);
                DataBody = dp.Substring(16, Convert.ToInt32(DataLength));
            }
            catch (Exception ex)
            {
                other.WriteErrLog("解码报文内容", ex);
            }
        }

        /// <summary>
        /// 执行反射机制,运行指定方法
        /// </summary>
        /// <param name="nspace"></param>
        /// <param name="fname"></param>
        public string[] reflectCore(string nspace, string fname)
        {
            try
            {
                Type t = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "\\BLL.dll").GetType(nspace);
                object[] ps = new object[1];
                ps[0] = DataBody;
                object emp = t.Assembly.CreateInstance(nspace);
                MethodInfo mi = t.GetMethod(fname);
                return (string[])mi.Invoke(emp, ps);
            }
            catch (NullReferenceException ex)
            {
                other.WriteErrLog("执行反射机制,运行指定方法", ex);
                return null;
            }
        }

        #region Setter AND Getter

        /// <summary>
        /// 获取操作命令
        /// </summary>
        public commandType CmdType
        {
            get
            {
                if (_cmdType.ToString().Length != 4)
                {
                    CmdType = commandType.c000;
                }
                return _cmdType;
            }
            set { _cmdType = value; }
        }

        /// <summary>
        /// 获取语句密钥
        /// </summary>
        public string DataKey
        {
            get
            {
                if (_dataKey.Length != 8)
                {
                    return "12345678";
                }
                else
                {
                    return _dataKey;
                }

            }
            set { _dataKey = value; }
        }
        /// <summary>
        /// 获取报文主体
        /// </summary>
        string DataLength
        {
            get
            {
                if (Convert.ToInt32(_dataLength) >= 10000 || Convert.ToInt32(_dataLength) <= 0)
                {
                    return "9999";
                }
                else
                {
                    return string.Format("{0:0000}", _dataLength);
                }
            }
            set { _dataLength = Convert.ToInt32(value); }
        }

        /// <summary>
        /// 报文结束标记
        /// </summary>
        public string DataEnd
        {
            get
            {
                if ((_dataEnd == null) || (_dataEnd == ""))
                {
                    return "]}";
                }
                else
                {
                    return _dataEnd;
                }
            }
            set { _dataEnd = value; }
        }

        public string DataBody
        {
            get { return _dataBody; }
            set { _dataBody = value; }
        }

        #endregion

    }
}
